<template>
  <div>
    <el-dialog v-if="enable" :visible.sync="enable" title="数据库编辑">
      <el-form ref="form" label-width="80px" :model="model" :rules="rules">
        <el-form-item label="数据库名">
          <el-input v-model="model.name"></el-input>
        </el-form-item>
        <el-form-item label="字符集">
          <el-select v-model="model.charset" filterable style="width:100%">
            <el-option v-for="charset in charsetList" :key="charset" :label="charset" :value="charset"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序规则">
          <el-select v-model="model.collation" style="width:100%">
            <el-option v-for="collation in collations" :key="collation" :label="collation" :value="collation"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="创建用户">
          <el-checkbox v-model="model.createUser" @change="actionCheckboxCreateUserChanged"></el-checkbox>
        </el-form-item>
        <el-form-item label="用户名" v-if="model.createUser">
          <el-input v-model="model.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" v-if="model.createUser">
          <el-input v-model="model.password"></el-input>
        </el-form-item>
        <el-form-item label="主机" v-if="model.createUser">
          <el-input v-model="model.host"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="actionCreate">创建</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Common from '../../utils/Common.js'
export default {
    name : 'MysqlDatabaseCreateDialog',
    data() {
        return {
            enable : false,
            connection : null,
            charsetList : [],
            collationList : {},
            model : {},
            rules : {},
        };
    },
    computed: {
        collations() {
            if ( undefined == this.model.charset ) {
                return [];
            }
            return this.collationList[this.model.charset];
        },
    },
    methods : {
        // 打开对话框
        async open( connection ) {
            this.connection = connection;

            this.charsetList = [];
            let charsets = await this.connection.execute('show character set');
            for ( let i=0; i<charsets.length; i++ ) {
                this.charsetList.push(charsets[i]['Charset']);
            }

            this.collationList = {};
            let collations = await this.connection.execute('SELECT * FROM information_schema.COLLATIONS');
            for ( let i=0; i<collations.length; i++ ) {
                if ( undefined == this.collationList[collations[i]['CHARACTER_SET_NAME']] ) {
                    this.collationList[collations[i]['CHARACTER_SET_NAME']] = [];
                }
                this.collationList[collations[i]['CHARACTER_SET_NAME']].push(collations[i]['COLLATION_NAME']);
            }

            this.model = {};
            this.enable = true;
        },

        // 创建数据库
        async actionCreate() {
            let sql = [`CREATE DATABASE \`${this.model.name}\``];
            if ( !Common.isVarEmpty(this.model.charset) ) {
                sql.push(`CHARACTER SET '${this.model.charset}'`);
            }
            if ( !Common.isVarEmpty(this.model.collation) ) {
                sql.push(`COLLATE '${this.model.collation}'`);
            }
            sql = sql.join(' ');

            try {
                await this.connection.execute(sql);
            } catch ( e ) {
                this.$message.error(e.message);
                return;
            }

            try {
                sql = `CREATE USER '${this.model.username}'@'${this.model.host}' IDENTIFIED BY '${this.model.password}'`;
                await this.connection.execute(sql);
                sql = `GRANT ALL ON \`${this.model.name}\`.* TO '${this.model.username}'@'${this.model.host}'`;
                await this.connection.execute(sql);
                sql = 'FLUSH PRIVILEGES';
                await this.connection.execute(sql);
            } catch ( e ) {
                this.$message.error(e.message);
                return;
            }

            this.$emit('created');
            this.enable = false;
        },

        // 创建用户勾选变更
        actionCheckboxCreateUserChanged() {
            if ( this.model.createUser && undefined == this.model.username ) {
                this.model.username = this.model.name;
                this.model.password = this.model.name;
                this.model.host = 'localhost';
            }
        },
    }
}
</script>